package modelo;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Session;



import hbt.AbstractHibernateManager;
import entities.IngredienteEntity;
import entities.PlatoEntity;
import entities.ProductoEntity;

public class PlatoDao extends AbstractHibernateManager {

	private static PlatoDao instancia = null;
	
	public static PlatoDao getInstancia() {
		if(instancia == null) {
			instancia = new PlatoDao();
		}
		return instancia;
	}
	
	public void save (PlatoEntity plato){
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.persist(plato);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[Plato PERSISTED ON TABLE Platos]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
	}
	
	public void grabar (PlatoEntity plato){
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.save(plato);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[Plato SAVED ON TABLE Platos]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
	}
	
	
	public PlatoEntity obtenerPlato(Integer id) {
		Session s = null;
		PlatoEntity platoE = new PlatoEntity();
		
		try {
			s = getNewSession();
			platoE =  (PlatoEntity) s.createQuery("FROM PlatoEntity plato " +
									"WHERE plato.id = :id")
							  .setInteger("id", id).uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return platoE;
	}

	
}
